package com.itheima.demo3.controller;

import com.itheima.demo3.dto.ApiResponse;
import com.itheima.demo3.dto.LoginRequest;
import com.itheima.demo3.dto.RegisterRequest;
import com.itheima.demo3.entity.User;
import com.itheima.demo3.service.UserService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import jakarta.validation.Valid;
import java.util.HashMap;
import java.util.Map;

/**
 * 认证控制器
 */
@RestController
@RequestMapping("/api/auth")
@RequiredArgsConstructor
@Slf4j
public class AuthController {
    
    private final UserService userService;
    
    /**
     * 用户注册
     * @param request 注册请求
     * @return 注册结果
     */
    @PostMapping("/register")
    public ApiResponse<String> register(@Valid @RequestBody RegisterRequest request) {
        log.info("User registration request: {}", request.getUsername());
        
        String result = userService.register(request);
        
        if ("User registered successfully".equals(result)) {
            return ApiResponse.success(result);
        } else {
            return ApiResponse.error(400, result);
        }
    }
    
    /**
     * 用户登录
     * @param request 登录请求
     * @return 登录结果
     */
    @PostMapping("/login")
    public ApiResponse<Map<String, Object>> login(@Valid @RequestBody LoginRequest request) {
        log.info("User login request: {}", request.getUsername());
        
        String result = userService.login(request);
        
        if ("Login successful".equals(result)) {
            // 获取用户信息
            User user = userService.getUserByUsername(request.getUsername());
            
            // 构造响应数据
            Map<String, Object> data = new HashMap<>();
            data.put("message", result);
            
            // 构造用户信息（处理null值）
            Map<String, Object> userInfo = new HashMap<>();
            userInfo.put("id", user.getId());
            userInfo.put("username", user.getUsername());
            userInfo.put("email", user.getEmail());
            userInfo.put("nickname", user.getNickname());
            userInfo.put("phone", user.getPhone());
            userInfo.put("avatar", user.getAvatar());
            userInfo.put("createTime", user.getCreateTime());
            
            data.put("user", userInfo);
            
            return ApiResponse.success(data);
        } else {
            return ApiResponse.error(401, result);
        }
    }
    
    /**
     * 获取用户信息
     * @param username 用户名
     * @return 用户信息
     */
    @GetMapping("/user/{username}")
    public ApiResponse<Map<String, Object>> getUserInfo(@PathVariable String username) {
        log.info("Get user info request: {}", username);
        
        User user = userService.getUserByUsername(username);
        if (user == null) {
            return ApiResponse.error(404, "User not found");
        }
        
        // 构造响应数据（不包含密码）
        Map<String, Object> userInfo = new HashMap<>();
        userInfo.put("id", user.getId());
        userInfo.put("username", user.getUsername());
        userInfo.put("email", user.getEmail());
        userInfo.put("nickname", user.getNickname());
        userInfo.put("phone", user.getPhone());
        userInfo.put("avatar", user.getAvatar());
        userInfo.put("status", user.getStatus());
        userInfo.put("createTime", user.getCreateTime());
        userInfo.put("updateTime", user.getUpdateTime());
        
        return ApiResponse.success(userInfo);
    }
    
    /**
     * 健康检查
     * @return 健康状态
     */
    @GetMapping("/health")
    public ApiResponse<String> health() {
        return ApiResponse.success("Service is running");
    }
} 